<?php
namespace app\staff\controller;
use think\Db;
use think\Request;
use think\Exception;
use app\common\controller\Common;
class Goods extends Common
{
	protected $keyword_search = ['og.goods_name'];//关键字
	protected $model = null;
	protected $order = 'og.goods_sn desc';

	/**
	* 产品销售业绩
	*/
	public function index()
	{
		$this->model = new \app\staff\model\GoodsCount();
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');

				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
				$search = $this->keyword_search($search_arr);
				$order = $this->order;
				$where = $this->handleList($data);
				/*@列表*/
				$list = $this->model->getGoodsCount($where, $order, $page, $row, $search);
				// echo Db::getlastsql();
				$res['list'] = collection($list)->toArray();
				/*@列表总条数*/
				$con = $this->model->tjGoodsCount($where, $order, $search);
				/*@数据统计*/
				$count = $this->model->allGoodsCount($where, $order, $search);
				$res['count'] = collection($count)->toArray();
				return $this->jsonList(0, config('status.SUCCESS_ONE'), $con, $res, $page, $row);
			}
			/*@产品分类*/
			$cate = model('Category')->where('fid',0)->select();
			$this->assign('cate',$cate);
			return $this->fetch('');
		}
	}
	/**
	 * 获取子级分类
	 */
	public function sun_cate()
	{
		if(Request()->isPost()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				if($data['ty'] == 0){
					$info = model('Category')->where('fid',$data['fid'])->select();
				}else{
					$info = model('Category')->getAttributeList($data['fid']);
				}
				$this->success($info);
			}
		}
	}
	/************************************ 私有 *********************************************/

	/**
	* 列表数据处理
	*/
	protected function handleList($data)
	{	
		$where = [];
		/*@登录者id*/
		switch(session('admin.admin_type')){
			case 0: $where = [];break;/*总部*/
			case 1: $where['o.shop_id'] = session('admin.shop_id');break;/*店长*/
			case 2: $where['o.admin_id'] = session('admin.admin_id');break;/*营业员*/
			default: return false;break;
		}
		/*@已支付订单*/
		$where['o.state'] = 1;
		/*@产品分类*/
		if(isset($data['category_id']) && $data['category_id'] != ''){
			$where['og.category_id'] = $data['category_id'];
		}
		/*@产品子分类*/
		if(isset($data['cat_id']) && $data['cat_id'] != ''){
			$where['og.cat_id'] = $data['cat_id'];
		}
		/*@产品品牌*/
		if(isset($data['brand_name']) && $data['brand_name'] != ''){
			$where['og.brand_name'] = $data['brand_name'];
		}
		/*@产品内存*/
		if(isset($data['memory_name']) && $data['memory_name'] != ''){
			$where['og.memory_name'] = $data['memory_name'];
		}
		/*@产品属性*/
		if(isset($data['attribute_name']) && $data['attribute_name'] != ''){
			$where['og.attribute_name'] = $data['attribute_name'];
		}
		/*@产品颜色*/
		if(isset($data['colour_name']) && $data['colour_name'] != ''){
			$where['og.colour_name'] = $data['colour_name'];
		}
		/*@时间区间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$where['o.addtime'] = [
				['egt', strtotime($data['start'])],
				['lt', strtotime($data['end'])]
			];
		}else if(isset($data['start']) && $data['start'] != ''){
			$where['o.addtime'] = ['egt', strtotime($data['start'])];
		}else if(isset($data['end']) && $data['end'] != ''){
			$where['o.addtime'] = ['lt', strtotime($data['end'])];
		}
		return $where;
	}
}